<?php
/*
 * SoloPHP PHP框架
 * $Author: Yotianya <yotianya@gmail.com>
 * $Dtime:2011-10-24
 * $Copyright: CopyRight By SoloPHP Yotianya
 */

class Controller
{

	public $View = NULL;

	/** 
	 *初始化视图
	 */
	public function __construct(){

		$this->view = new View();
	}
	/**
	 * 设置模板硬式导入变量
	 */
	public function A($key,$value='') {

		$this->view->assign($key,$value);
	}
 	/**
 	 * 调用视图方法
 	 */
	public function V($file){

		$this->view->show($file);
	}
 	/**
 	 * 数据过滤,数据检查
 	 */
	public function F(){
		$filter =  new  Filter();
		return $filter;
	}
	/**
	 * 加载Model模型
	 */
	public function M($modelName){
		
		$modelFile = APP_PATH . '/Model/' . $modelName . 'Model.class.php';			//模型类文件绝对路径
		$mFile = '/Model/' . $modelName . 'Model.class.php';						//模型类文件相对路径
		!file_exists($modelFile) && errorMsg ( 'modelNotExist',$mFile);				//如果模型类文件不存在提示错误
		include_once($modelFile);													//存在, 则引入模型类
		$class = ucwords($modelName.'Model');										//获得模型类名
		!class_exists($class) && errorMsg ( 'modelNotClass',$mFile.'未定义'.$class);	//判断是否定义了模型类, 如果没有提示错误
		$model = new $class();														//实例化模型类
		return $model;																//返回实例
	}
	/**
	 * 环境变量处理
	 */
	public function D($name = null, $method = null){

			if($name != null){
				if(!isset($_REQUEST[$name])){
					if( null != $method ){
					switch (strtolower($method)) {
						case 'get':
							return $_GET[$name];
						case 'post':
							return $_POST[$name];
						case 'cookie':
							return $_COOKIE[$name];
						case 'session':
							return $_SESSION[$name];
						case 'server':
							return $_SERVER[$name];
						}
					}
					return false;
				}
					return $_REQUEST[$name];
				}

					return false;
		}
	/*
	 * 运行框架
	 */
	public function Run(){
		$url_args = explode("/", $_SERVER['REQUEST_URI']);
		$url_sort = array();
		(isset($GLOBALS['C']['Common']['ReWrite']) && $GLOBALS['C']['Common']['ReWrite'])?$rewrite=0:$rewrite=1;//判断ReWrite伪静态
		for($u = 1; $u < count($url_args); $u++){
			if($u == $rewrite){
				$url_sort['d'] = $url_args[$u];
			}
			elseif($u == $rewrite+1){
				$url_sort['c'] = $url_args[$u];
			}
			elseif($u == $rewrite+2){
				$url_sort['a'] = $url_args[$u];
			}
			else {
				$url_sort[$url_args[$u]] = isset($url_args[$u+1]) ? $url_args[$u+1] : "";$u+=1;}
		}
		if("POST" == strtoupper($_SERVER['REQUEST_METHOD'])){
			$_REQUEST = $_POST =  $_POST + $url_sort;
		}else{
			$_REQUEST = $_GET = $_GET + $url_sort;
		}

	   $control = (!isset($_REQUEST['c'])||$_REQUEST['c']==null)?$GLOBALS['C']['Common']['Control']:trim($_REQUEST['c']);
       $action  = (!isset($_REQUEST['a'])||$_REQUEST['a']==null)?$GLOBALS['C']['Common']['Action']:trim($_REQUEST['a']);
	   $GLOBALS['url'] = $url_sort;

       $actiondir=(APP_FILE=='index')?'default/':APP_FILE.'/';
	   $controlFile = APP_PATH . '/Controller/' .$actiondir.$control . '.class.php';

		if(!file_exists($controlFile))                                //如果文件不存在提示错误, 否则引入
		{
			errorMsg ( 'controlNotExist', $controlFile);
		}
		include($controlFile);
		$class = ucwords($control);	                                  //将控制器名称中的每个单词首字母大写,来当作控制器的类名
		if(!class_exists($class))                                     //判断类是否存在, 如果不存在提示错误
		{
			errorMsg ( 'classError', $class);
		}
		$instance = new $class();	                                  //否则创建实例
		if(!method_exists($instance, $action))	                      //判断实例$instance中是否存在$action方法, 不存在则提示错误
		{
			errorMsg ( 'methodNotExist', $action);
		}
		$instance->$action();
	}
}